<template>
  <el-dialog
    class="add-progress-model"
    title="进度设置"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    :visible="visible"
    :width="$setRem(600)"
    :before-close="handleCancel">
    <div class="form-container">
      <el-form
        :model="ruleForm"
        :rules="rules"
        :label-width="$setRem(160)"
        ref="addProgressForm"
        class="rule-form">
        <el-form-item label="任务名称" prop="scheduleName" required>
          <el-input
            v-if="addType === 'add' || addType === 'edit'"
            placeholder="请输入任务名称"
            v-model="ruleForm.scheduleName"
            clearable>
          </el-input>
          <el-input
            v-else
            :disabled="true"
            placeholder="请输入任务名称"
            :value="ruleForm.scheduleName">
          </el-input>
        </el-form-item>
        <el-form-item
          v-if="addType === 'add' || addType === 'edit'"
          label="计划维护"
          prop="planTime"
          required>
          <el-date-picker
            v-model="ruleForm.planTime"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            type="daterange"
            clearable
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item
          v-if="addType === 'feedback'"
          label="实际反馈开始日期"
          prop="actualStartTime">
          <el-date-picker
            v-model="ruleForm.actualStartTime"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="开始日期"
            type="date">
          </el-date-picker>
        </el-form-item>
        <el-form-item
          v-if="addType === 'feedback'"
          label="实际反馈结束日期"
          prop="actualEndTime">
          <el-date-picker
            v-model="ruleForm.actualEndTime"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="结束日期"
            type="date">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleCancel">取 消</el-button>
      <el-button
        class="success"
        type="primary"
        :loading="loading"
        @click="handleSubmit">
        提 交
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    loading: {
      type: Boolean,
      default: false
    },
    // 计划类型
    addType: {
      type: String,
      default: ''
    },
    // 计划类型
    scheduleType: {
      type: String,
      default: ''
    },
    formData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      ruleForm: {
        id: '',
        scheduleName: '',
        planTime: [],
        actualStartTime: '',
        actualEndTime: '',
      },
      // 校验规则
      rules: {
        scheduleName: [
          { required: true, message: '请填写进度名称' },
          { trigger: 'blur' }
        ],
        planTime: [
          { required: true, type: 'array', message: '请选择计划时间' }
        ]
      }
    };
  },
  watch: {
    visible(val) {
      if (!val) {
        this.ruleForm = {
          id: '',
          scheduleName: '',
          planTime: [],
          actualStartTime: '',
          actualEndTime: '',
        };
      }
    },
    formData: {
      deep: true,
      handler(data) {
        if (data) {
          if (!data || !Object.values(data).length) return;
          const { planStartTime, planEndTime } = data;
          Object.keys(this.ruleForm).forEach(key => {
            if (key === 'planTime') {
              planStartTime && this.ruleForm[key].push(planStartTime);
              planEndTime && this.ruleForm[key].push(planEndTime);
              return;
            }
            this.ruleForm[key] = data[key];
          });
        }
      }
    }
  },
  methods: {
    // 取消
    handleCancel() {
      this.$emit('cancel', false);
    },
    handleSubmit() {
      this.$refs.addProgressForm.validate(valid => {
        if (valid) {
          this.$emit('submit', this.ruleForm);
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
  @import '../../../assets/styles/dialog';

  .add-progress-model {
    .form-container {
      /deep/ .el-form-item {
        .el-form-item__label {
          color: #fff;
        }
        .el-form-item__content {
          .el-input, .el-textarea {
            width: 340px;
          }
        }
      }
    }
  }
</style>
